# http://www.cbad.cnki.net/
from django.http import HttpResponse
import json
import requests
from bs4 import BeautifulSoup
import re
import time

proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }

#每次拿去最新的cookies
def updateCookie(cookies,tempCookies):

    for k, v in tempCookies.items():
        cookies[k] = v

    return cookies

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data


def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    trs = soup.select(".color1")[1:]

    for tr in trs:

        tempData = resultData()

        try:
            tempData["titleName"] = tr.select(".book")[0].text.strip()
        except:
            pass

        try:
            tempData["titleHref"] = tr.select(".book")[0]["href"]
        except:
            pass

        try:
            tempData["source"] = tr.select("td")[4].text.strip()
        except:
            pass

        try:
            tempData["publishTime"] = tr.select("td")[6].text.strip()
        except:
            pass

        allData.append(tempData)



    return allData

def buildingParameters(data):
    # 主题、题名或关键词、题名、第一作者、作者单位、作者、关键词、摘要、基金、DOI
    # 主题:()*作者:()
    typeCorr = {
        "SU": "主题",
        "TI": "题名",
        "AU": "作者",
        "AF": "作者单位",
        "AB": "摘要",
        "KY": "关键词",
    }
    logCorr = {
        "and": "*",
        "or": "+",
        "not": "^",
        "###":"###"
    }

    data["log"].append("###")

    tempStr = []
    for i in range(len(data["values"])):

        try:
            strr = typeCorr[data["type"][i]] + ":" + "(" + data["values"][i] + ")" + "{}".format(logCorr[data["log"][i]])
            tempStr.append(strr)
        except:
            continue

    strr = ''
    for value in tempStr:
        strr += value

    return strr.strip().strip("#").strip()


def getData(keyword,cookies):

    headers = {
        'Origin': 'http://www.cbad.cnki.net',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Accept': '*/*',
        'Referer': 'http://www.cbad.cnki.net/BookInfo/BookListIndex',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
    }

    data = {
        'c_data': '',
        'c_count': '0',
        'ischeckall': 'false',
        'ischecknow': '',
        'pageindex': '',
        'Groupname': '',
        'Groupvalue': '',
        'isSession': '0',
        'datafrom': '',
        're_allsubject': '0,1,101,102,103,2,201,202,203,204,205,206,207,208,209,210,211,3,301,302,303,304,305,306,307,308,4,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,5,501,6,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,7,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,8,801,802,803,804,805,806,807,808,809,810,9,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,10,1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1011,1012,1013,11,1101,1102,1103,1104,1105,1106,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,1117,1118,1119,1120,1121,1122,12,1201,13,1301,1302,1303,1304,1305,1306,14,1401,1402,1403,1404,1405,1406,1407,1408,15,1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,1515,16,1601,1602,1603,1604,1605,1606,1607,1608,1609,1610,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,17,1701,1702,1703,1704,1705,1706,1707,1708,1709,1710,18,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823,1824,1825,1826,1827,1828,1829,1830,1831,1832,1833,1834,1835,1836,1837,1838,1839,1840,1841,1842,1843,1844,1845,19,1901,1902,1903,1904,1905,20,2001,21,2101,2102,2103,2104,2105,2106,2107,2108,2109,22,2201,2202,2203,2204,2205,2206,2207',
        're_booktype': '1,11,12,13,14,15,16,17,18,19,20',
        're_startyear': '1949',
        're_endyear': '2019',
        're_Province': 'all',
        're_key': '1,',
        're_keyv1': "{},".format(keyword),
        're_keyv2': '',
        're_unk': '',
        're_un': '',
        're_islike': '=,',
        'ishcited': '0',
        'isbcb': '0',
        'allsubject': '0,1,101,102,103,2,201,202,203,204,205,206,207,208,209,210,211,3,301,302,303,304,305,306,307,308,4,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,5,501,6,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,7,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,8,801,802,803,804,805,806,807,808,809,810,9,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,10,1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1011,1012,1013,11,1101,1102,1103,1104,1105,1106,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,1117,1118,1119,1120,1121,1122,12,1201,13,1301,1302,1303,1304,1305,1306,14,1401,1402,1403,1404,1405,1406,1407,1408,15,1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,1515,16,1601,1602,1603,1604,1605,1606,1607,1608,1609,1610,1611,1612,1613,1614,1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,17,1701,1702,1703,1704,1705,1706,1707,1708,1709,1710,18,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823,1824,1825,1826,1827,1828,1829,1830,1831,1832,1833,1834,1835,1836,1837,1838,1839,1840,1841,1842,1843,1844,1845,19,1901,1902,1903,1904,1905,20,2001,21,2101,2102,2103,2104,2105,2106,2107,2108,2109,22,2201,2202,2203,2204,2205,2206,2207',
        'booktype': '1,11,12,13,14,15,16,17,18,19,20',
        'startyear': '1949',
        'endyear': '2019',
        'f_startyear': '1949',
        'f_endyear': '2019',
        'Province': 'all',
        'SearchType': '1',
        'key1': keyword,
        'sislike': '=',
        'resultSearch': '0',
        'key': '1,',
        'keyv1':"{},".format(keyword),
        'unk': '',
        'un': '',
        'islike': '=,',
        'num': '10',
        'WebPagerPageIndex': '1',
        'allcheck': 'false',
        'checknow': ''
    }

    response = requests.post('http://www.cbad.cnki.net/BookInfo/AjaxBookList', headers=headers,data=data,proxies = proxies,cookies = cookies)

    result= formData(response)


    return result


def getOneCookies():

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }
    s = requests.session()

    response = s.get('http://www.cbad.cnki.net/', headers=headers,proxies = proxies)


    cookies = s.cookies.get_dict()

    return cookies


def index(request):
    data = json.loads(request.POST.get('data', '1'))

    cookies = getOneCookies()

    #
    # cookiesT,url = getCookies(data["values"][0],cookies)
    #
    # cookies = updateCookie(cookies,cookiesT)

    data = getData(data["values"][0],cookies)

    return HttpResponse(json.dumps({
        "collection": data,
        "allNum": 0
    }), content_type="application/json")


